package com.cloudfast.oauth.endpoint;

import java.security.Principal;
import java.security.SecureRandom;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cloudfast.common.result.ApiResponse;
import com.cloudfast.context.CloudSecurity;
import com.cloudfast.context.error.ErrorCode;
import com.cloudfast.oauth.token.CloudTokenStore;
import com.cloudfast.redis.CloudRedisTemplate;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(value = "登入登出接口", tags = {"登入登出接口"})
@RestController
public class CloudFrameEndpoint {
    private static final Logger logger = LoggerFactory.getLogger(Logger.class);

    @Autowired
    private RedisConnectionFactory redisConnectionFactory;

    @Autowired
    private CloudSecurity cloudSecurity;

    @Autowired
    private CloudRedisTemplate cloudRedisTemplate;

    /**
     * @说明:获取当前登入的用户
     * @时间:2019年4月1日 下午1:58:27
     * @开发人员:liuyinwei1510
     * @参数:@param principal
     * @参数:@return
     */
    @GetMapping(value = "/loginUser")
    @ApiOperation("获取当前登入的用户-内部使用")
    public Principal getUser(Principal principal) {

        return principal;
    }

    /**
     * 获取系统配置
     *
     * @return
     */
    @PostMapping(value = "/cloud/oauth/conf")
    public ApiResponse conf() {
        Map<String, Object> map = new HashMap<>();
        /**
         * public key
         */
        map.put("PublicKey", cloudSecurity.getPublicKey());
        /**
         * 开启验证码
         */
        map.put("open_captcha", cloudSecurity.isCaptcha());
        return ApiResponse.success(map);
    }

    /**
     * 退出请求
     */
    @PostMapping(value = "/cloud/oauth/logout")
    public ApiResponse logout(HttpServletRequest request) {
        try {
            String accessToken = request.getHeader("authorization");
            CloudTokenStore tokenStore = new CloudTokenStore(redisConnectionFactory);
            tokenStore.removeAccessToken(accessToken.replace("bearer", "").trim());
            return ApiResponse.success("登出成功");
        } catch (Exception e) {
            logger.error("登出:{}", e.getMessage());
            return ApiResponse.message(ErrorCode.SYSTEM_ERROR);
        }
    }

    /**
     * 获取登入短信验证码
     *
     * @param phone
     * @return
     */
    @PostMapping(value = "/cloud/oauth/sms_code")
    public ApiResponse sendSmsCode(String phone, HttpServletRequest request) {
        try {
            SecureRandom random = new SecureRandom();
            int smsCode = random.nextInt(10000);
            cloudRedisTemplate.set(phone, smsCode, 300);
            return ApiResponse.success(smsCode);
        } catch (Exception e) {
            return ApiResponse.message(ErrorCode.SYSTEM_ERROR);
        }
    }

    /**
     * 生成captcha 验证码
     *
     * @param request
     * @return
     */
    @PostMapping(value = "/cloud/oauth/captcha")
    public ApiResponse sendCaptchaCode(HttpServletRequest request) {
        try {
            SecureRandom random = new SecureRandom();
            int smsCode = random.nextInt(10000);
            cloudRedisTemplate.set(request.getSession().getId(), smsCode, 300);
            return ApiResponse.success(smsCode);
        } catch (Exception e) {
            return ApiResponse.message(ErrorCode.SYSTEM_ERROR);
        }
    }

}
